package com.fanhao.dllo.justforyou.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.activity.JokeMessageCommentActivity;
import com.fanhao.dllo.justforyou.entity.JokeMessageList;
import com.fanhao.dllo.justforyou.volley.GetVolleyDatasUtil;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;

import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class JokeMessageAdapter extends RecyclerView.Adapter<
        JokeMessageAdapter.JokeMessageViewHolder> {
    private List<JokeMessageList> list;
    //
//    VolleySingleton volleySingleton = VolleySingleton.getInstance();
//    private ImageLoader.ImageListener imageListener;
//    private ImageLoader imageLoader;
    private Context context;
    private JokeMessageCommentOnClickListener listener;

    public interface JokeMessageCommentOnClickListener {
        void onItemClickListener(JokeMessageList list);
    }

//    public void setOnClickListener(JokeMessageOnClickListener jcl) {
//        listener = jcl;
//    }

    public JokeMessageAdapter(Context c) {
//     imageLoader = volleySingleton.getImageLoader();
        this.context = c;

    }


    // 添加数据
    public void addData(List<JokeMessageList> datas) {
        this.list = datas;
        notifyDataSetChanged();

    }

    public void loadData(List<JokeMessageList> datas) {
        list.addAll(datas);
        notifyDataSetChanged();
    }

    /**
     * 缓存组件内部类
     **/
    class JokeMessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imgIv;
        private TextView nameTv, timeTv, contentTv, likeTv,
                dislikeTv, shareTv, commentTv;
        private int position;

        public JokeMessageViewHolder(View v) {
            super(v);

            imgIv = (ImageView) v.findViewById(R.id.imgIv);

            nameTv = (TextView) v.findViewById(R.id.nameTv);
            timeTv = (TextView) v.findViewById(R.id.timeTv);
            contentTv = (TextView) v.findViewById(R.id.contentTv);
            likeTv = (TextView) v.findViewById(R.id.likeTv);
            dislikeTv = (TextView) v.findViewById(R.id.dislikeTv);
            shareTv = (TextView) v.findViewById(R.id.shareTv);
            commentTv = (TextView) v.findViewById(R.id.commentTv);

            commentTv.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.commentTv:
                    if (list != null && list.size() > 0) {
                        JokeMessageList messageList = list.get(position);
                        Intent intent = new Intent(context, JokeMessageCommentActivity.class);
                        intent.putExtra("jokeMesId", messageList.getId());
                        context.startActivity(intent);

                    }

                    break;
            }
        }
    }

    @Override
    public int getItemCount() {

        return list != null && list.size() > 0 ?
                list.size() : 0;
    }

    @Override
    public void onBindViewHolder(JokeMessageViewHolder holder, int position) {
        // 进行页面控制的网络获取
        if (list != null && list.size() > 0) {
            JokeMessageList jokeMessageList = list.get(position);
            holder.position = position;

            holder.nameTv.setText(jokeMessageList.getName());
            holder.timeTv.setText(jokeMessageList.getCreate_time());
            holder.contentTv.setText(jokeMessageList.getText());
            holder.likeTv.setText(jokeMessageList.getLove());
            holder.dislikeTv.setText(jokeMessageList.getHate());
            holder.shareTv.setText(jokeMessageList.getRepost());
            holder.commentTv.setText(jokeMessageList.getComment());


            if (jokeMessageList.getProfile_image() != null && jokeMessageList.getProfile_image().length() > 0) {
                String imgUrl = jokeMessageList.getProfile_image();
                GetVolleyDatasUtil.getBitmapRequest(imgUrl, holder.imgIv);
            }

        }

    }


//    private void getBitmap( String url, ImageLoader.ImageListener imageListener) {

//        imageListener = ImageLoader.getImageListener(imgIv,R.mipmap.ic_launcher,R.mipmap.ic_launcher);

//        imageLoader.get(url, imageListener);

//      }


    /**
     * 绑定布局
     * */
    @Override
    public JokeMessageViewHolder onCreateViewHolder(
            ViewGroup parent, int position) {
        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());

        View v = inflater.inflate(R.layout.fragment_joke_message_item,null);
        return new JokeMessageViewHolder(v);
    }
}
